<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\api\service\Token;
use app\api\model\PetsDiary as PetsDiaryModel;

class PetsDiary extends Api
{
    public function index() {
        $diary = model('PetsDiary')->limit(4)->order('sort asc')->select();
        // $res = [];
        // if($diary) {
        //     foreach ($diary as $k => $v) {
        //         $res[$k]['pets_id'] = $v['pets_id'];
        //         $res[$k]['avatar'] = $v['pets']['avatar'];
        //         $res[$k]['year'] = $v['year'];
        //         $res[$k]['month'] = $v['month'];
        //         $res[$k]['content'] = $v['content'];
        //     }
        // }
        return $diary;
    }

    public function create()
    {
        $uid = Token::getCurrentUid();
        $data = input('post.');
        $res = PetsDiaryModel::create([
            'year' => $data['age'][0],
            'month' => $data['age'][1],
            'content' => $data['content'],
            'img' => $data['img'],
            'pets_id' => $data['pets_id'],
            'uid' => $uid,
        ]);
        if($res) {
            return [
                'code' => 1,
                'info' => '发布成功'
            ];
        }else{
            return [
                'code' => 0,
                'error' => '发布失败'
            ];
        }
    }

    public function update($id) {
        $data = input('post.');
        $diaryData = [
            'year' => $data['age'][0],
            'month' => $data['age'][1],
            'content' => $data['content'],
            'img' => $data['img'],
            'pets_id' => $data['pets_id'],
        ];
        $diary = model('PetsDiary')->save($diaryData, ['id' => $id]);
        if($diary) {
            return ['code' => 1, 'info' => '更新成功！'];
        }else{
            return ['code' => 0, 'error' => '更新失败！'];
        }
    }

    public function info($id) {
        $diary = model('PetsDiary')->where(['id' => $id])->find();
        return $diary ? $diary : [];
    }

    public function del($id) {
        $diary = model('PetsDiary')->where(['id' => $id])->find();
        if($diary) {
            $file = ROOT_PATH . 'web' . str_replace(config('web_site_url'), '', $diary['img']);
            if(file_exists($file)) {
                @unlink($file);
            }
            $re = model('PetsDiary')->where(['id' => $id])->delete();
            if($re) {
                return ['code' => 1, 'info' => '删除成功！'];
            }else{
                return ['code' => 0, 'error' => '删除失败！'];
            }
        }else{
            return ['code' => 0, 'error' => '未找到数据！'];
        }

    }

    public function list()
    {
        $uid = Token::getCurrentUid();
        $diary = PetsDiaryModel::with('pets')->where(['uid' => $uid])->order('year asc,month asc')->select();
        $re = [];
        if($diary) {
            foreach ($diary as $k => $v) {
                $re[$k]['avatar'] = $v['img'];
                $re[$k]['id'] = $v['id'];
                $re[$k]['year'] = $v['year'];
                $re[$k]['month'] = $v['month'];
                $re[$k]['content'] = $v['content'];
            }
        }
        return $re;
    }

    public function getlist($id)
    {
        $uid = Token::getCurrentUid();
        $diary = PetsDiaryModel::with('pets')->where(['pets_id' => $id])->order('year asc,month asc')->select();
        $re = [];
        if($diary) {
            foreach ($diary as $k => $v) {
                $re[$k]['id'] = $v['id'];
                $re[$k]['avatar'] = $v['img'];
                $re[$k]['year'] = $v['year'];
                $re[$k]['month'] = $v['month'];
                $re[$k]['content'] = $v['content'];
                if($v['pets']['uid'] == $uid) {
                    $re[$k]['mine'] = 1;
                }else{
                    $re[$k]['mine'] = 0;
                }
            }
        }
        return $re;
    }
}
